﻿$("#srch-term").keypress(searchKeyPress);
function searchKeyPress(event) {
    if (event.which == 13) {
        getSearchItems();
    }
}
$('#mainSearch').click(getSearchItems);
$('#span-keyword').on('click', '.span-keyword-item', function () {
    var searchText = $(this).text();
    sendSearchTextToServer(searchText);
});
function getSearchItems() {
    var searchText = $('#srch-term').val();
    sendSearchTextToServer(searchText);
}

function sendSearchTextToServer(searchText) {
    if (searchText != "") {
        $('.menu-brand').removeClass('menu-choose');
        $('.menu-cate').removeClass('menu-choose');
        $('#mpItemPage').val(1);
        $.cookie('u_brand', 0);
        $.cookie('u_cate', 0);
        $.cookie('ctr', 21);
        $.cookie('u_find', searchText);
        $(".main-item").remove();
        getItems();
        $.get('Handler/HandlerSearch.ashx', { status: 'Writekeyword', search: searchText }, function (data) { });
    }
}

//keyword
$("#span-keyword").ready(function () {
    $.get('Handler/HandlerSearch.ashx', { status: 'keyword' },
            function (data) {
                for (var i = 0; i <= data.length - 1; i++) {
                    var elem = '<input type="hidden" class="page-ctr" value="5" /><span class="span-keyword-item">' + data[i] + '</span>';
                    $("#span-keyword").append(elem);
                }
                //$('#keyword').autocomplete({ source: data });
            });
})


$("#srch-term").keyup(function () {
    var searchText = $("#srch-term").val();
    //$('.search-list').empty(); //不清掉會一直加上去
    if (searchText != '') {
        //去後台把資料讀出來 加進去搜尋列表
        $.get('Handler/HandlerSearch.ashx', { status: 'searchText', search: searchText },
            function (data) {
                $('#srch-term').autocomplete({ source: data });
            });
    }
});

// Sign Out
$('#toolBarUser').on('click', '#signOut', signOut);